package com.kxxnzstdsw.modle.interceptor

import com.kxxnzstdsw.modle.entity.BaseEntity
import com.kxxnzstdsw.modle.entity.BaseEntityDraft
import org.babyfish.jimmer.kt.isLoaded
import org.babyfish.jimmer.sql.DraftInterceptor
import java.time.LocalDateTime

/**拦截器用于更新updateAt与creatAt字段*/
class BasicEntityDraftInterceptor : DraftInterceptor<BaseEntity, BaseEntityDraft> {
  override fun beforeSave(draft: BaseEntityDraft, original: BaseEntity?) {
    if (!isLoaded(draft, BaseEntity::updateAt)) {
      draft.updateAt = LocalDateTime.now()
    }
    if (original === null) {
      if (!isLoaded(draft, BaseEntity::createAt)) {
        draft.createAt = LocalDateTime.now()
      }
    }
  }
}
